##EnosHershReplication.r
###master document for:
#Enos, Ryan D. and Eitan D. Hersh. 2015. “Party Activists as Campaign Advertisers: The Ground Campaign
#as a Principal-Agent Problem,” American Political Science Review 109 (2):252–278
###RdE June 2015 (renos@gov.harvard.edu)


##remove data
rm(list = ls())

options(scipen=999)  ##turn off scientific notation

##load libraries
library(foreign)
library(gdata)
library(RColorBrewer)
library(MatchIt)  
library(nlme)
library(xtable)
library(Rmisc)
library(maptools)
library(raster)
library(rgdal)
library(colorspace)
library(gplots)
library(epicalc)
library(Gmisc)
library(grid)

##VARIABLES USED REPEATEDLY IN ANALYSIS
var.names = c('very.liberal','moderate','strong.partisan','vote.2010','female','white','black','hispanic','college.track','non.church','income.recode','age')
##BATTLE GROUND STATES
battles = c('FL','CO','IA','NV','NH','NC','OH','VA','WI')

##GRAY COLOR GOR GRAPHS
gray.color = colors()[320]


##LOAD DATA
load('EnosHershData.RData')

###CREATE VERSIONS OF DATA FOR EASE OF USE IN ANALYSIS BELOW
stack.unds.dat = stack.dat[stack.dat$undecided == 1,]
stack.dems.dat = stack.dat[stack.dat$dem == 1,]

CCES.dems.dat = CCES.dat[CCES.dat$dem == 1,]
CCES.unds.dat = CCES.dat[CCES.dat$undecided == 1,]

CCAP.com.dems.dat = CCAP.com.dat[CCAP.com.dat$dem == 1,]
CCAP.com.unds.dat = CCAP.com.dat[CCAP.com.dat$undecided == 1,]

GCP.downballot.dat = GCP.dat[GCP.dat$committee != 'Organizing for America',] ##non-Obama workers
GCP.dat = GCP.dat[GCP.dat$committee == 'Organizing for America',] ##limit to Obama



######################################
####MATCHED SET OF DATA TO BE USED BELOW
#########################################
match.stack.dat = stack.dems.dat[,c(var.names,'state')] ##subset variables for matching, 
match.GCP.dat = GCP.dat[,c(var.names,'perm.state')]
colnames(match.GCP.dat) = c(var.names,'state') ##rename columans so Rbind works 
colnames(match.stack.dat) = c(var.names,'state') 
match.stack.dat$stack = 1  ##create variable to designate which data set it comes from, this is useful below
match.GCP.dat$stack = 0
match.dat = rbind(match.stack.dat,match.GCP.dat)
match.dat = match.dat[complete.cases(match.dat),]  ##find compelte cases for use with matching, cannot match incomplete cases
out.match = matchit(stack ~ vote.2010+strong.partisan+female+white+black+hispanic+college.track+non.church+income.recode+age+state, 
                    data = match.dat, 
                    method = 'cem'
                    )
out.match.GCP.dat = match.data(out.match, group = 'control') ##extract matched GCP data
out.match.stack.dat = match.data(out.match) ##extract all matched data


##########################################################################
##########################################################################
##########################################################################
##FIGURE 1, PAGE 259

cat('Creating Figure 1... \n \n \n')

map.dat = GCP.dat

##RENAMING AND RECODING VARIABLES IS NECESSARY TO MATCH WITH EXTERNAL DATA BELOW
map.dat$camp.state = as.character(map.dat$camp.state)
state.rename.list = list('AL' = 'Alabama', 'AK' = 'Alaska', 'AZ' = 'Arizona', 'AR' = 'Arkansas', 'CA' = 'California', 'CO' = 'Colorado', 'CT' = 'Connecticut', 'DE' = 'Delaware', 'DC' = 'District of Columbia',
                         'FL' = 'Florida', 'GA' = 'Georgia', 'HI' = 'Hawaii', 'ID' = 'Idaho', 'IL' = 'Illinois', 'IN' = 'Indiana', 'IA' = 'Iowa', 'KS' = 'Kansas', 'KY' = 'Kentucky', 'LA' = 'Louisiana',
                         'ME' = 'Maine', 'MD' = 'Maryland', 'MA' = 'Massachusetts', 'MI' = 'Michigan', 'MN' = 'Minnesota', 'MS' = 'Mississippi', 'MO' = 'Missouri', 'MT' = 'Montana', 'NE' = 'Nebraska', 
                         'NV' = 'Nevada', 'NH' = 'New Hampshire', 'NJ' = 'New Jersey', 'NM' = 'New Mexico', 'NY' = 'New York', 'NC' = 'North Carolina', 'ND' = 'North Dakota', 'OH' = 'Ohio', 'OK' = 'Oklahoma',
                         'OR' = 'Oregon', 'PA' = 'Pennsylvania', 'PR' = 'Puerto Rico', 'RI' = 'Rhode Island', 'SC' = 'South Carolina', 'SD' = 'South Dakota', 'TN' = 'Tennessee', 'TX' = 'Texas', 
                         'UT' = 'Utah', 'VT' = 'Vermont', 'VA' = 'Virginia', 'WA' = 'Washington', 'WV' = 'West Virginia', 'WI' = 'Wisconsin', 'WY' = 'Wyoming'
                        )


##CALL IN SHAPE FILES, NEED COUNTIES AND STATES BECAUSE COUNTIES ARE USED TO CONSTRUCT MICHIGAN WITH NO WATER
us1 <- getData('GADM', country="USA", level=1)
us2 <- getData('GADM', country="USA", level=2)

##extract Michigan from counties to use below, remove bodies of water
mi2 = us2[us2$NAME_1 == 'Michigan' & us2$TYPE_2!='Water body',]
##remove michigan because of odd shape, use trick below to recombine.  
us1 = us1[us1$NAME_1 != 'Michigan',]
##merge the Michigan counties into a single new state, which makes it easier to work with below
mi2 = unionSpatialPolygons(mi2,ID = mi2$ID_1)

## Specify a geographic extent for the map
## by defining the top-left and bottom-right geographic coordinates
left = -118; top = 50; right = -83; bottom = 22
mapExtent <- rbind(c(left, top), c(right,bottom))

## Specify the required projection using a proj4 string
newProj <- CRS("+proj=poly +lat_0=0 +lon_0=-100 +x_0=0 
               +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

## Project the map extent (first need to specify that it is longlat) 
nonproj.coords = SpatialPoints(mapExtent, proj4string=CRS("+proj=longlat"))
mapExtentPr <- spTransform(SpatialPoints(mapExtent, 
                             proj4string=CRS("+proj=longlat")),
                             newProj)

top.proj = bbox(mapExtentPr)['coords.x2','max'] 
left.proj = bbox(mapExtentPr)['coords.x1','min']
bottom.proj = bbox(mapExtentPr)['coords.x2','min'] 
right.proj = bbox(mapExtentPr)['coords.x1','max']   	  

## Project other layers
us1Pr <- spTransform(us1, newProj) 
mi2Pr <- spTransform(mi2, newProj) 

###set color elements for use below
zero.color = 'gray87'  ##color to represent zero below

##cycle through and collect relevant information from data, use this below when cycling through states	
state.totals = table(map.dat$camp.state)
state.totals = reshape::rename(state.totals,replace = state.rename.list)

##append states with no activity by hand
zero.state.names = us1Pr$NAME_1[!us1Pr$NAME_1%in%names(state.totals)]
zero.states = rep(0,length(zero.state.names))
names(zero.states) = zero.state.names

state.totals = c(state.totals,zero.states)

##find quantiles of states for activity, making this map into quantiles:
map.quantiles = quantile(state.totals)
use.colors = brewer.pal(9,"Blues")[c(3,5,7,9)]

pdf('output/figure_1.pdf',
    width = 7*(800/540), height = 7
    )
  par(bg = 'transparent')
  par(mar = c(.5,0,.5,0))
  plot(mapExtentPr, pch=NA)
  plot(us1Pr, border="white", col= use.colors[1], add=TRUE)
  plot(mi2Pr, border="white", col = zero.color, add=TRUE)

  ## Highlight states of interest
  for(j in 1:length(state.totals)){
    state.share = state.totals[j] 
    use.name = names(state.totals)[j]
    ##designate color by quantile
    if(state.share <= map.quantiles[2]){use.color = use.colors[1]}
    if(state.share > map.quantiles[2] & state.share <= map.quantiles[3]){use.color = use.colors[2]}
    if(state.share > map.quantiles[3] & state.share <= map.quantiles[4]){use.color = use.colors[3]}
    if(state.share > map.quantiles[4]){use.color = use.colors[4]}
    
    ###if state is Michigan, use Michigan data created above
    if(use.name == 'Michigan'){
      plot(mi2Pr, border="gray", col = use.color, add=TRUE)
      }
    else{
      plot(us1Pr[us1Pr$NAME_1 == names(state.totals)[j],], border="gray", col = use.color, add=TRUE)
      }
    }
  ##legend
  legend(x = "bottomleft",
         legend = c('Top Quartile',
                    '2nd Quartile',
                    '3rd Quartile',
                    'Bottom Quartile'),
         fill = rev(use.colors),
         box.col = 'white',
         border = 'white',
         cex = 1.7,
         xjust = 1,
         yjust = .5,
         title = 'Campaign Worker Locations',
         bty = 'n'
      )
  
  dev.off()


##########################################################################
##########################################################################
############################################################################
###CLAIMS ON PAGE 259 AND PAGE 266
###WORKERS IN A A MORE CONSERVATIVE STATE AND PERCENT MOVING TO BATTLEGROUND STATES

GCP.dat.comp.state = subset(GCP.dat, is.na(GCP.dat$perm.state) == F & is.na(GCP.dat$camp.state) == F) ##make dataset with only complete state data for this purpose
GCP.dat.comp.state$movers = ifelse(GCP.dat.comp.state$perm.state != GCP.dat.comp.state$camp.state, T, F) ##identify movers
GCP.dat.comp.state$battleground = ifelse(GCP.dat.comp.state$perm.state %in% battles,T,F)
GCP.dat.comp.state$battleground.camp = ifelse(GCP.dat.comp.state$camp.state %in% battles,T,F)


prev.dat.reduced = vote.dat[,c('state','X2008')]
colnames(prev.dat.reduced) = c('state','perm.state.obama.2008')
GCP.dat.comp.state = merge(GCP.dat.comp.state,prev.dat.reduced, 
                           by.x = 'perm.state',
                           by.y = 'state',
                           all.x = T,
                           all.y = F)
colnames(prev.dat.reduced) = c('state','camp.state.obama.2008')
GCP.dat.comp.state = merge(GCP.dat.comp.state,prev.dat.reduced, 
                           by.x = 'camp.state',
                           by.y = 'state',
                           all.x = T,
                           all.y = F)


sink('output/r_output.txt')
cat('Claim on Page 259:\n What percent of workers worked in a more conservative state than their home state: \n')
print(nrow(GCP.dat.comp.state[GCP.dat.comp.state$movers == T & (GCP.dat.comp.state$perm.state.obama.2008 > GCP.dat.comp.state$camp.state.obama.2008),])/nrow(GCP.dat.comp.state[GCP.dat.comp.state$movers == T,]))
cat('\n \n \n')
cat('Claim on Page 266:\n What percent of workers worked in state other than home, excluding home battleground states: \n')
print(nrow(GCP.dat.comp.state[GCP.dat.comp.state$movers == T & GCP.dat.comp.state$battleground == F,])/nrow(GCP.dat.comp.state[GCP.dat.comp.state$battleground == F,]))
cat('\n \n \n')
cat('Claim on Page 266:\n What percent of workers in battleground states were from from out of state: \n')
print(nrow(GCP.dat.comp.state[GCP.dat.comp.state$movers == T & GCP.dat.comp.state$battleground.camp == T,])/nrow(GCP.dat.comp.state[GCP.dat.comp.state$battleground.camp == T,]))
cat('\n \n \n')
sink()


##########################################################################
##########################################################################
##########################################################################
###CLAIM ON PAGE 259
###AVERAGE AMOUNT OF TIME PER WEEK WORKING ON CAMPAIGN
sink('output/r_output.txt', append = T)
cat('Claim on Page 259:\n Average amount of campaigning per week:\n')
print(mean(GCP.dat$time.rc, na.rm=T))
cat('\n \n \n')
sink()


##########################################################################
##########################################################################
############################################################################

###CLAIM ON PAGE 259
###FOCUS OF CAMPAIGN
sink('output/r_output.txt', append = T)
cat('Claim on Page 259:\n What percent of workers were involved in voter contact: \n')
print(nrow(subset(GCP.dat,GCP.dat$strategy_3 == 1 | GCP.dat$strategy_4 == 1))/nrow(subset(GCP.dat,is.na(GCP.dat$strategy_1) == F | is.na(GCP.dat$strategy_2) == F | is.na(GCP.dat$strategy_3) == F | is.na(GCP.dat$strategy_4) == F | is.na(GCP.dat$strategy_5) == F | is.na(GCP.dat$strategy_6) == F | is.na(GCP.dat$strategy_7) == F | is.na(GCP.dat$strategy_8) == F)))
cat('\n \n \n')
sink()

##########################################################################
##########################################################################
############################################################################

###CLAIM ON PAGE 260
###PERSUASIAN AS PRIMARY GOAL
sink('output/r_output.txt', append = T)
cat('Claim on Page 260:\n What percent of workers involved in voter contact say that persuasion is the goal: \n')
##how are volunteers spending their time?
print(nrow(subset(GCP.dat,GCP.dat$strategy_3 == 1))/nrow(subset(GCP.dat,is.na(GCP.dat$strategy_3) == F | is.na(GCP.dat$strategy_4) == F)))
cat('\n \n \n')
sink()


##########################################################################
##########################################################################
############################################################################
###TABLE 1, PAGE 261
###TABLE A1, P 271 
###FOOTNOTE 16, PAGE 260
cat('Creating Table 1 and A1... \n \n \n')


outmat = as.data.frame(matrix(nrow = length(var.names), ncol = 7)) ##storage matrix
outmat.t.test = as.data.frame(matrix(nrow = length(var.names), ncol = 4)) ##storage matrix for t.tests
##loop through vars, put each in table:
for(i in 1:length(var.names)){
  outmat[i,1] = var.names[i]
  outmat[i,2] = mean(stack.dat[,var.names[i]], na.rm = T)
  outmat[i,3] = mean(stack.unds.dat[,var.names[i]], na.rm = T)
  outmat[i,4] = mean(stack.dems.dat[,var.names[i]], na.rm = T)
  outmat[i,5] = mean(GCP.dat[,var.names[i]], na.rm = T)
  outmat[i,6] = mean(out.match.stack.dat[,var.names[i]], na.rm = T)
  outmat[i,7] = mean(out.match.GCP.dat[,var.names[i]], na.rm = T)
  

  ###FOOTNOTE 16, PAGE 260    
  ##make table of t.test results too	
  outmat.t.test[i,1] = var.names[i]
  outmat.t.test[i,2] = t.test(stack.dat[,var.names[i]], GCP.dat[,var.names[i]])$p.value
  outmat.t.test[i,3] = t.test(stack.unds.dat[,var.names[i]], GCP.dat[,var.names[i]])$p.value
  outmat.t.test[i,4] = t.test(stack.dems.dat[,var.names[i]], GCP.dat[,var.names[i]])$p.value
  } 

outmat.t.test[,2:ncol(outmat.t.test)] = round(outmat.t.test[,2:ncol(outmat.t.test)],3)
colnames(outmat.t.test) = c('variable','Electorate','Undecideds','Democrats')

sink('output/r_output.txt', append = T)
cat('Footnote 16, Page 260: \n P-values of T-tests for difference of means between workers and voters in Table 1: \n')
print(outmat.t.test)
cat('\n \n \n')
sink()

colnames(outmat) = c('variable','Electorate','Undecideds','Democrats','Workers','Matched.democrats','Matched.elites')

##turn into latex table
outmat[,2:ncol(outmat)] = round(outmat[,2:ncol(outmat)],3)
outmat[1:10,2:ncol(outmat)] =  outmat[1:10,2:ncol(outmat)]*100  
outmat.print = rbind(outmat,c('N',nrow(stack.dat),nrow(stack.unds.dat),nrow(stack.dems.dat), nrow(GCP.dat),nrow(out.match.stack.dat),nrow(out.match.GCP.dat))) ##add N.  


###TABLE 1, PAGE 261
outmat.print.regulars = outmat.print[,c(1:5)]
outtable = xtable(outmat.print.regulars)
print(outtable,
      file = 'output/table_1.tex',
      include.rownames = F)	

###TABLE A1, P 271 
outmat.print.matched = outmat.print[,c(1,6,7)]
outtable = xtable(outmat.print.matched)
print(outtable,
      file = 'output/table_A1.tex',
      include.rownames = F)	




##########################################################################
##########################################################################
############################################################################
###FIGURE 2, PAGE 260
cat('Creating Figure 2... \n \n \n')

##table of flows of workers from state to state
red.sequence = seq(from = 5, to = 255, by = 5) ##sequences of numbers for rgb below
blue.sequence = seq(from = 255, to = 5, by = -5)
liberal.colors = rgb(red.sequence,0,blue.sequence,max = 255)

flow.table = table(GCP.dat$perm.state,GCP.dat$camp.state)
for(i in rownames(flow.table)){
  flow.table[i,i] = 0
    }
jpeg('output/figure_2.jpg',
     height = 1000,
     width = 500)

  transitionPlot(flow.table,
                 fill_start_box = liberal.colors,
                 fill_end_box = liberal.colors,
                 #arrow_clr = liberal.colors,
                 min_lwd = unit(.25, "mm"), 
                 max_lwd = unit(8, "mm"),
                 overlap_order = nrow(flow.table):1, ##this puts ordering of top, "most liberal" states on top
                 cex = 1,
                 type_of_arrow = "gradient",
                 tot_spacing = 0.025
            )
  dev.off()


##########################################################################
##########################################################################
############################################################################
###FIGURE 3, PAGE 262 and FOOTNOTE 17, PAGE 261
cat('Creating Figure 3... \n \n \n')


###ideology
mean.1 = mean(as.numeric(stack.dat$ideology.recode.1),na.rm = T)
cis.1 = ci(as.numeric(stack.dat$ideology.recode.1))

mean.2 = mean(as.numeric(stack.unds.dat$ideology.recode.1), na.rm = T)
cis.2 = ci(as.numeric(stack.unds.dat$ideology.recode.1))

mean.3 = mean(as.numeric(stack.dems.dat$ideology.recode.1), na.rm = T)
cis.3 = ci(as.numeric(stack.dems.dat$ideology.recode.1))

mean.4 = mean(as.numeric(GCP.dat$ideology.recode.1), na.rm = T)
cis.4 = ci(as.numeric(GCP.dat$ideology.recode.1))

names.args = c('Very \n Liberal', 'Somewhat \n Liberal', 'Moderate','Somewhat \n Conservative', 'Very \n Conservative')
ylims = c(0,.7)
names.cex = 1.15
lab.cex = .95
point.cex = 1.25
text.cex = 1
offset = .04 ##offset for point text below
pdf('output/figure_3.pdf',
    height = 9, width = 6.5)

par(mar = c(4,3,4,0))
par(mfrow = c(5,1))
par(las = 1)
par(bty = 'n')
bars.1 = barplot(table(stack.dat$ideology.recode.1)/nrow(subset(stack.dat,is.na(stack.dat$ideology.recode.1) == F)),
                 xlab = '',
                 xaxt = 'n',
                 col = gray.color,
                 ylim = ylims,
                 border = 'NA',
                 yaxt = 'n')
title(main = 'Electorate',
      cex.main = 2)
mtext(names.args[c(1,2,4,5)], side = 1, at = bars.1[c(1,2,4,5)],
      cex = lab.cex,
      line = 2)
mtext(names.args[3], side = 1, at = bars.1[3],
      cex = lab.cex,
      line = .65)
axis(side = 2,
     at = seq(from = ylims[1], to = ylims[2], by = .2)
    )
barplot(table(stack.unds.dat$ideology.recode.1)/nrow(subset(stack.unds.dat,is.na(stack.unds.dat$ideology.recode.1) == F)),
        xlab = '',
        xaxt = 'n',
        col = gray.color,
        ylim = ylims,
        border = 'NA',
        yaxt = 'n')
title(main = 'Undecided Voters',
      cex.main = 2)
mtext(names.args[c(1,2,4,5)], side = 1, at = bars.1[c(1,2,4,5)],
      cex = lab.cex,
      line = 2)
mtext(names.args[3], side = 1, at = bars.1[3],
      cex = lab.cex,
      line = .65)
axis(side = 2,
     at = seq(from = ylims[1], to = ylims[2], by = .2)
)

barplot(table(stack.dems.dat$ideology.recode.1)/nrow(subset(stack.dems.dat,is.na(stack.dems.dat$ideology.recode.1) == F)),
        xlab = '',
        xaxt = 'n',
        col = gray.color,
        ylim = ylims,
        border = 'NA',
        yaxt = 'n')
title(main = 'Democrats',
      cex.main = 2)
mtext(names.args[c(1,2,4,5)], side = 1, at = bars.1[c(1,2,4,5)],
      cex = lab.cex,
      line = 2)
mtext(names.args[3], side = 1, at = bars.1[3],
      cex = lab.cex,
      line = .65)
axis(side = 2,
     at = seq(from = ylims[1], to = ylims[2], by = .2)
)
barplot(table(GCP.dat$ideology.recode.1)/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F)),
        xlab = '',
        xaxt = 'n',
        col = gray.color,
        ylim = ylims,
        border = 'NA',
        yaxt = 'n')
title(main = 'Campaign Workers',
      cex.main = 2)
mtext(names.args[c(1,2,4,5)], side = 1, at = bars.1[c(1,2,4,5)],
      cex = lab.cex,
      line = 2)
mtext(names.args[3], side = 1, at = bars.1[3],
      cex = lab.cex,
      line = .65)
axis(side = 2,
     at = seq(from = ylims[1], to = ylims[2], by = .2)
)
par(mar = c(4,3,2,0)) ##reset margins for final plot, 3rd argument is top
plot(0:4,0:4,
     type = 'n',
     ylim = c(.5,4.5),
     xlim = c(1,5),
     xlab = '',
     ylab = '',
     yaxt = 'n',
     xaxt = 'n'
)
axis(side = 1,
     at = seq(from = 1, to = 5, by = .5))

points(mean.1,
       4,
       col = gray.color,
       pch = 18,
       cex = point.cex)
text(mean.1 + offset,
     4,
     'Electorate',
     pos = 4,
     cex = text.cex)
segments(x0 = cis.1$lower95ci, x1 = cis.1$upper95ci,
         y0 = 4, y1 = 4,
         col = gray.color)

points(mean.2,
       3,
       col = gray.color,
       pch = 18,
       cex = point.cex)	
text(mean.2 + offset,
     3,
     'Undecided Voters',
     pos = 4,
     cex = text.cex)
segments(x0 = cis.2$lower95ci, x1 = cis.2$upper95ci,
         y0 = 3, y1 = 3,
         col = gray.color)

points(mean.3,
       2,
       col = gray.color,
       pch = 18,
       cex = point.cex)	
text(mean.3 + offset,
     2,
     'Democrats',
     pos = 4,
     cex = text.cex)
segments(x0 = cis.3$lower95ci, x1 = cis.3$upper95ci,
         y0 = 2, y1 = 2,
         col = gray.color)

points(mean.4,
       1,
       col = gray.color,
       pch = 18,
       cex = point.cex)	
text(mean.4 + offset,
     1,
     'Campaign Workers',
     pos = 4,
     cex = text.cex)
segments(x0 = cis.4$lower95ci, x1 = cis.4$upper95ci,
         y0 = 1, y1 = 1,
         col = gray.color)

dev.off()


###FOOTNOTE 17, PAGE 261
sink('output/r_output.txt', append = T)
cat('Footnote 17, Page 261:\n Wilcox-Mann-Whitney Test for distribution shift of 0 between workers and voters in Figure 3: \n')
##wilcox test for differences
cat('\n Workers v Electorate:\n')
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1),
                  as.numeric(stack.dat$ideology.recode.1)))
cat('\n Workers v Undecideds:\n')
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1),
                  as.numeric(stack.unds.dat$ideology.recode.1)))
cat('\n Workers v Democrats:\n')
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1),
                  as.numeric(stack.dems.dat$ideology.recode.1)))
cat('\n \n \n')
sink()






##########################################################################
##########################################################################
##########################################################################
###FIGURE 4, PAGE 263 and FOOTNOTE 23, PAGE 264
cat('Creating Figure 4...\n \n \n')

##reorder factor for plotting
GCP.voter.iss.rc.table = table(GCP.dat$voter_iss.rc)[order(table(GCP.dat$voter_iss.rc), decreasing = T)] #order table by counts to get ranking
GCP.voter.iss.rc.ordering = names(GCP.voter.iss.rc.table) ##extract names
GCP.dat$voter_iss.rc  = factor(GCP.dat$voter_iss.rc, GCP.voter.iss.rc.ordering) ##use the names argument from above to order new factor
negative.phrase.GCP = "I dont have regular interaction with voters" ##I'm taking this out repeatedly

GCP.most.imp.vote.N = length(GCP.dat$voter_iss.rc[is.na(GCP.dat$voter_iss.rc)==F & GCP.dat$voter_iss.rc != negative.phrase.GCP])

GCP.vote.table = table(GCP.dat$voter_iss.rc)/GCP.most.imp.vote.N
##drop unneeded phrase
GCP.vote.table = GCP.vote.table[grep('voters',names(GCP.vote.table),invert = T)]

###CCES
CCES.dat.YAL321.table = table(CCES.dat$YAL321)[order(table(CCES.dat$YAL321), decreasing = T)] #order table by counts to get ranking
CCES.dat.YAL321.table.ordering = names(CCES.dat.YAL321.table) ##extract names
CCES.dat$YAL321.rc  = factor(CCES.dat$YAL321, CCES.dat.YAL321.table.ordering) ##use 

##now do the same for the CCES.dat dems data
CCES.dat.dems.YAL321.table = table(CCES.dems.dat$YAL321)[order(table(CCES.dems.dat$YAL321), decreasing = T)] #order table by counts to get ranking
CCES.dat.dems.YAL321.table.ordering = names(CCES.dat.dems.YAL321.table) ##extract names
CCES.dems.dat$YAL321.rc  = factor(CCES.dems.dat$YAL321, CCES.dat.dems.YAL321.table.ordering) ##use 

##now do the same for the CCES.dat unds data
CCES.dat.unds.YAL321.table = table(CCES.unds.dat$YAL321)[order(table(CCES.unds.dat$YAL321), decreasing = T)] #order table by counts to get ranking
CCES.dat.unds.YAL321.table.ordering = names(CCES.dat.unds.YAL321.table) ##extract names
CCES.unds.dat$YAL321.rc  = factor(CCES.unds.dat$YAL321, CCES.dat.unds.YAL321.table.ordering) ##use 


negative.phrase.CCES.dat = "I don't have regular interaction with voters" 

CCES.dat.most.imp.vote.N = length(CCES.dat$YAL321.rc[is.na(CCES.dat$YAL321.rc)==F & CCES.dat$YAL321.rc != negative.phrase.CCES.dat])
CCES.dat.dems.most.imp.vote.N = length(CCES.dems.dat$YAL321.rc[is.na(CCES.dems.dat$YAL321.rc)==F & CCES.dems.dat$YAL321.rc != negative.phrase.CCES.dat])
CCES.dat.unds.most.imp.vote.N = length(CCES.unds.dat$YAL321.rc[is.na(CCES.unds.dat$YAL321.rc)==F & CCES.unds.dat$YAL321.rc != negative.phrase.CCES.dat])


CCES.dat.vote.table = table(CCES.dat$YAL321.rc)/CCES.dat.most.imp.vote.N
CCES.dat.dems.vote.table = table(CCES.dems.dat$YAL321.rc)/CCES.dat.dems.most.imp.vote.N
CCES.dat.unds.vote.table = table(CCES.unds.dat$YAL321.rc)/CCES.dat.unds.most.imp.vote.N

##do same for most important personal issue
GCP.most.imp.rc.table = table(GCP.dat$most_imp.rc)[order(table(GCP.dat$most_imp.rc), decreasing = T)] #order table by counts to get ranking
GCP.most.imp.rc.ordering = names(GCP.most.imp.rc.table) ##extract names
GCP.dat$most_imp.rc  = factor(GCP.dat$most_imp.rc, GCP.most.imp.rc.ordering) ##use the names argument from above to order new factor
negative.phrase.GCP = "I dont have regular interaction with voters" ##I'm taking this out repeatedly

GCP.most.imp.vote.N = length(GCP.dat$most_imp.rc[is.na(GCP.dat$most_imp.rc)==F & GCP.dat$most_imp.rc != negative.phrase.GCP])
GCP.most.table = table(GCP.dat$most_imp.rc)/GCP.most.imp.vote.N
##drop unneeded phrase
GCP.most.table = GCP.most.table[grep('voters',names(GCP.most.table),invert = T)]

##sub out weird character, then drop all phrases including voter, because I don't need that
names(CCES.dat.vote.table) = sub('\x92t','',names(CCES.dat.vote.table))
names(CCES.dat.dems.vote.table) = sub('\x92t','',names(CCES.dat.dems.vote.table))
names(CCES.dat.unds.vote.table) = sub('\x92t','',names(CCES.dat.unds.vote.table))

CCES.dat.vote.table = CCES.dat.vote.table[grep('voters',names(CCES.dat.vote.table),invert = T)]
CCES.dat.dems.vote.table = CCES.dat.dems.vote.table[grep('voters',names(CCES.dat.dems.vote.table),invert = T)]
CCES.dat.unds.vote.table = CCES.dat.unds.vote.table[grep('voters',names(CCES.dat.unds.vote.table),invert = T)]

##take top 5
GCP.vote.table = GCP.vote.table[1:5]
GCP.most.table = GCP.most.table[1:5]
CCES.dat.vote.table = CCES.dat.vote.table[1:5]
CCES.dat.dems.vote.table =  CCES.dat.dems.vote.table[1:5]
CCES.dat.unds.vote.table =  CCES.dat.unds.vote.table[1:5]

pdf('output/figure_4.pdf',
    width = 9.5, height = 11)
  names.cex = 1.5 ##names text size
  par(mfrow = c(4,1)) ##number of plots
  par(las = 1)
  #par(las = 2) ##axis orientation
  xbars.1 = barplot(GCP.most.table,
                    ylim = c(0,.9),
                    cex.names = names.cex,
                    col = gray.color,
                    border = NA,
                    names = c('Economy','Inequality','Education','Health Care','Other'))
  text(y = GCP.most.table,
       x = xbars.1,
       round(GCP.most.table,2),
       cex = 1.25,
       pos = 3)
  title(	main = 'Issue Most Important to \n Campaign Workers',
         cex.main = 2)
  xbars.2 = barplot(GCP.vote.table,
                    ylim = c(0,.9),
                    cex.names = names.cex,
                    col = gray.color,
                    border = NA,
                    names = c('Economy','Health Care','Other','Corruption','Inequality'))
  text(y = GCP.vote.table,
       x = xbars.2,
       round(GCP.vote.table,2),
       cex = 1.35,
       pos = 3)
  title(	main = 'Issue Thought to be Most Important \n to Voters by Campaign Workers',
         cex.main = 2)
  
  xbars.3 = barplot(CCES.dat.dems.vote.table,
                    ylim = c(0,.9),
                    cex.names = names.cex,
                    col = gray.color,
                    border = NA,
                    names = c('Economy','Inflation','Health Care','Other','Corruption'))
  text(y = CCES.dat.dems.vote.table,
       x = xbars.3,
       round(CCES.dat.dems.vote.table,2),
       cex = 1.35,
       pos = 3)
  title(	main = 'Issue Thought to be Most Important \n to Other Voters by Democrat Voters',
         cex.main = 2)
  
  xbars.4 = barplot(CCES.dat.unds.vote.table,
                    ylim = c(0,.9),
                    cex.names = names.cex,
                    col = gray.color,
                    border = NA,
                    names = c('Economy','Inflation','Terrorism','Other','War'))
  text(y = CCES.dat.unds.vote.table,
       x = xbars.3,
       round(CCES.dat.unds.vote.table,2),
       cex = 1.35,
       pos = 3)
  title(	main = 'Issue Thought to be Most Important \n to Other Voters by Undecided Voters',
         cex.main = 2)

  dev.off()


###FOOTNOTE 23, PAGE 264
GCP.dat$economy.most.important.voters = ifelse(GCP.dat$voter_iss.rc == 'Economy/jobs',1,0)	
GCP.dat$economy.most.important.personal = ifelse(GCP.dat$most_imp.rc == 'The economy',1,0)
CCES.unds.dat$economy.most.important.voters = ifelse(CCES.unds.dat$YAL321 == 'Economy/jobs',1,0)
CCES.dems.dat$economy.most.important.voters = ifelse(CCES.dems.dat$YAL321 == 'Economy/jobs',1,0)

sink('output/r_output.txt', append = T)
cat('Footnote 23, Page 264: \n T-test for difference of means between workers and voters mentioning the economy in Figure 4:\n')

###t-test for difference in those mentioning economy  
cat('\n Most important personal issue for Workers versus perceptions for Democrast\n')
print(t.test(GCP.dat$economy.most.important.personal[GCP.dat$most_imp.rc != negative.phrase.GCP],CCES.dems.dat$economy.most.important.voters[CCES.dems.dat$YAL321.rc != negative.phrase.CCES.dat]))

cat('\n Most important personal issue for Workers versus perceptions for Undecideds\n')
print(t.test(GCP.dat$economy.most.important.personal[GCP.dat$most_imp.rc != negative.phrase.GCP],CCES.unds.dat$economy.most.important.voters[CCES.unds.dat$YAL321.rc != negative.phrase.CCES.dat]))

cat('\n Perceptions of most important issue for Workers versus perceptions for Democrats\n')
print(t.test(GCP.dat$economy.most.important.voters[GCP.dat$voter_iss.rc != negative.phrase.GCP],CCES.dems.dat$economy.most.important.voters[CCES.dems.dat$YAL321.rc != negative.phrase.CCES.dat]))

cat('\n Perceptions of most important issue for Workers versus perceptions for Undecideds\n')
print(t.test(GCP.dat$economy.most.important.voters[GCP.dat$voter_iss.rc != negative.phrase.GCP],CCES.unds.dat$economy.most.important.voters[CCES.unds.dat$YAL321.rc != negative.phrase.CCES.dat]))

cat('\n \n \n')
sink()





##########################################################################
##########################################################################
##########################################################################
###CLAIM ON PAGE 264, PERCENT RECRUITED DIRECTLY BY CAMPAIGN
sink('output/r_output.txt', append = T)
cat('Claim on Page 264:\n How are voters recruited to the campaign? Proportion table \n')
print(table(GCP.dat$inv_camp)/nrow(GCP.dat[is.na(GCP.dat$inv_camp)==F&GCP.dat$inv_camp!=6,]))
cat('\n \n \n')
sink()


##########################################################################
##########################################################################
##########################################################################
###FIGURE 5, PAGE 265 and FOOTNOTE 24, PAGE 264

cat('Creating Figure 5...\n \n \n')

##these set parameters for similar graphs below
ylims = c(-.2,.7) 
y1.pos = -.15
y2.pos = -.2

############################
bars = vector(length = 10)
odds = seq(from = 1, to = 10,by = 2)
evens = seq(from = 2, to = 10,by = 2)

bars[odds] = table(GCP.dat$ideology.recode.1[GCP.dat$inv_camp%in%c(1,3)])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$inv_camp%in%c(1,3)))
bars[evens] = table(GCP.dat$ideology.recode.1[GCP.dat$inv_camp==4])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$inv_camp==4))

mean.1 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp%in%c(1,3)]),na.rm = T)
cis.1 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp%in%c(1,3)]))

mean.2 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp==4]), na.rm = T)
cis.2 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp==4]))

pdf('output/figure_5.pdf')
  par(las = 1)
  par(bty = 'n')
  #par(mar = c(5,4,2,2))
  par(mar = c(4,3,2,0))
  
  bars.out = barplot2(bars,
                      xlab = '',
                      col = rep(c('black',gray.color),2),
                      ylim = ylims,
                      yaxt = 'n',
                      border = 'NA',
                      beside = T,
                      space = rep(c(.5,0),5))
  bars.places = (bars.out[odds]+bars.out[evens])/2
  text(y = 0,
       x = bars.places,
       labels = names.args,
       cex = .95,
       pos = 1)
  
  legend(x = 'topright',		
         legend = c('Volunteered', 'Recruited'),
         fill = c('black',gray.color),
         bty = 'n',
         cex = 1.25
      )
  
  par(new = T)
  plot(0,0,
       type = 'n',
       ylim = ylims,
       xlim = c(1,5),
       xlab = '',
       ylab = '',
       yaxt = 'n',
       xaxt = 'n'
      )
  axis(side = 2,
       at = seq(from = 0, to = ylims[2], by = .1))
  axis(side = 1,
       at = seq(from = 1, to = 5, by = .5))
  
  points(mean.1,
         y1.pos,
         col = 'black',
         pch = 18,
         cex = point.cex)
  segments(x0 = cis.1$lower95ci, x1 = cis.1$upper95ci,
           y0 = y1.pos, y1 = y1.pos)
  text(mean.1 + offset,
       y1.pos,
       'Volunteered',
       pos = 4,
       cex = text.cex)
  
  points(mean.2,
         y2.pos,
         col = gray.color,
         pch = 18,
         cex = point.cex)	
  segments(x0 = cis.2$lower95ci, x1 = cis.2$upper95ci,
           y0 = y2.pos, y1 = y2.pos,
           col = gray.color)
  text(mean.2 + offset,
       y2.pos,
       'Recruited',
       pos = 4,
       cex = text.cex)
    
  dev.off()

###FOOTNOTE 24, PAGE 264
sink('output/r_output.txt', append = T)
cat('Footnote 24, Page 264:\n Wilcox-Mann-Whitney Test for distribution shift of 0 between workers volunteering and workers recurited:\n')
##wilcox test for differences
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp%in%c(1,3)]),
                  as.numeric(GCP.dat$ideology.recode.1[GCP.dat$inv_camp==4])))
cat('\n \n \n')
sink()



##########################################################################
##########################################################################
##########################################################################
###FIGURE 6, PAGE 266 and FOOTNOTE 25, PAGE 265

cat('Creating Figure 6...\n \n \n')


bars = vector(length = 10)
odds = seq(from = 1, to = 10,by = 2)
evens = seq(from = 2, to = 10,by = 2)

bars[odds] = table(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(1,2)])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$camp_role==1 & GCP.dat$voters %in% c(1,2)))
bars[evens] = table(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(3,4)])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$camp_role==1 & GCP.dat$voters %in% c(3,4)))

mean.1 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(1,2)]),na.rm = T)
cis.1 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(1,2)]))

mean.2 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(3,4)]), na.rm = T)
cis.2 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(3,4)]))

pdf('output/figure_6.pdf')
  par(las = 1)
  par(bty = 'n')
  par(mar = c(4,3,2,0))
  
  bars.out = barplot2(bars,
                      xlab = '',
                      col = rep(c('black',gray.color),2),
                      ylim = ylims,
                      yaxt = 'n',
                      border = 'NA',
                      beside = T,
                      space = rep(c(.5,0),5))
  bars.places = (bars.out[odds]+bars.out[evens])/2
  text(y = 0,
       x = bars.places,
       labels = names.args,
       cex = .95,
       pos = 1)
  
  legend(x = 'topright',		
         legend = c('Majority of Time Not in Contact', 'Majority of Time in Contact'),
         fill = c('black',gray.color),
         bty = 'n',
         cex = 1.25
      )
  
  par(new = T)
  plot(0,0,
       type = 'n',
       ylim = ylims,
       xlim = c(1,5),
       xlab = '',
       ylab = '',
       yaxt = 'n',
       xaxt = 'n'
      )
  axis(side = 2,
       at = seq(from = 0, to = ylims[2], by = .1))
  axis(side = 1,
       at = seq(from = 1, to = 5, by = .5))
  
  points(mean.1,
         y1.pos,
         col = 'black',
         pch = 18,
         cex = point.cex)
  segments(x0 = cis.1$lower95ci, x1 = cis.1$upper95ci,
           y0 = y1.pos, y1 = y1.pos)
  text(mean.1 + offset,
       y1.pos,
       'Majority in Contact',
       pos = 4,
       cex = text.cex)
  
  points(mean.2,
         y2.pos,
         col = gray.color,
         pch = 18,
         cex = point.cex)	
  segments(x0 = cis.2$lower95ci, x1 = cis.2$upper95ci,
           y0 = y2.pos, y1 = y2.pos,
           col = gray.color)
  text(mean.2 + offset,
       y2.pos,
       'Majority Not in Contact',
       pos = 4,
       cex = text.cex)
  
  dev.off()


##FOOTNOTE 25, PAGE 265
sink('output/r_output.txt', append = T)
cat('Footnote 25, Page 265:\n Wilcox-Mann-Whitney Test for distribution shift of 0 between workers with majority of time in contact with voters and workers not majority of time in contact with voters:\n')
##wilcox test for differences
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(1,2)]),
                  as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp_role==1 & GCP.dat$voters %in% c(3,4)])))
cat('\n \n \n')
sink()


##########################################################################
##########################################################################
##########################################################################
###FIGURE 7, PAGE 265 and FOOTNOTE 26, PAGE 265

cat('Creating Figure 7...\n \n \n')

bars = vector(length = 10)
odds = seq(from = 1, to = 10,by = 2)
evens = seq(from = 2, to = 10,by = 2)


bars[odds] = table(GCP.dat$ideology.recode.1[GCP.dat$strategy_3 == 1])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$strategy_3 == 1))
bars[evens] = table(GCP.dat$ideology.recode.1[GCP.dat$strategy_4 == 1])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$strategy_4 == 1))

mean.1 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_3 == 1]),na.rm = T)
cis.1 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_3 == 1]))

mean.2 = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_4 == 1]), na.rm = T)
cis.2 = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_4 == 1]))

pdf('output/figure_7.pdf')
  par(las = 1)
  par(bty = 'n')
  par(mar = c(4,3,2,0))
  bars.out = barplot2(bars,
                      xlab = '',
                      col = rep(c('black',gray.color),2),
                      ylim = ylims,
                      yaxt = 'n',
                      border = 'NA',
                      beside = T,
                      space = rep(c(.5,0),5))
  bars.places = (bars.out[odds]+bars.out[evens])/2
  text(y = 0,
       x = bars.places,
       labels = names.args,
       cex = .95,
       pos = 1)
  
  legend(x = 'topright',		
         legend = c('Persuasion', 'Mobilization'),
         fill = c('black',gray.color),
         bty = 'n',
         cex = 1.25
  )
  
  par(new = T)
  plot(0,0,
       type = 'n',
       ylim = ylims,
       xlim = c(1,5),
       xlab = '',
       ylab = '',
       yaxt = 'n',
       xaxt = 'n'
  )
  axis(side = 2,
       at = seq(from = 0, to = ylims[2], by = .1))
  axis(side = 1,
       at = seq(from = 1, to = 5, by = .5))
  
  points(mean.1,
         y1.pos,
         col = 'black',
         pch = 18,
         cex = point.cex)
  segments(x0 = cis.1$lower95ci, x1 = cis.1$upper95ci,
           y0 = y1.pos, y1 = y1.pos)
  text(mean.1 + offset,
       y1.pos,
       'Persuasion',
       pos = 4,
       cex = text.cex)
  
  points(mean.2,
         y2.pos,
         col = gray.color,
         pch = 18,
         cex = point.cex)	
  segments(x0 = cis.2$lower95ci, x1 = cis.2$upper95ci,
           y0 = y2.pos, y1 = y2.pos,
           col = gray.color)
  text(mean.2 + offset,
       y2.pos,
       'Mobilization',
       pos = 4,
       cex = text.cex)
  
  dev.off()



###FOOTNOTE 26, PAGE 265
sink('output/r_output.txt', append = T)
cat('Footnote 26, Page 265:\n Wilcox-Mann-Whitney Test for distribution shift of 0 between workers workers citing persuasion and workers citing mobilization as primary strategy:\n')
##wilcox test for differences
print(wilcox.test(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_3 == 1]),
                  as.numeric(GCP.dat$ideology.recode.1[GCP.dat$strategy_4 == 1])))
cat('\n \n \n')
sink()




##########################################################################
##########################################################################
##########################################################################
###FIGURES 8, PAGE 268 AND A4, PAGE 273

cat('Creating Figures 8 and A4...\n \n \n')

ylims = c(0,.7)
point.cex = 2
text.cex = .9
lab.cex = 1
axis.cex = 2.5	

for(i in battles){
  offset = ifelse(i == 'OH', .12,.25) ##offset for point text below
  
  pdf(paste('output/figure_8_A4_',i,'.pdf',sep=''),
      width = 7, height = 5.5)
    par(mar = c(2,3,2,0))
    par(mfrow = c(2,1))
    par(las = 1)
    par(bty = 'n')
      
    mean.und = mean(as.numeric(stack.unds.dat$ideology.recode.1[stack.unds.dat$state == i]),na.rm = T)
    cis.und = ci(as.numeric(stack.unds.dat$ideology.recode.1[stack.unds.dat$state == i]))
    
    mean.dem = mean(as.numeric(stack.dems.dat$ideology.recode.1[stack.dems.dat$state == i]),na.rm = T)
    cis.dem = ci(as.numeric(stack.dems.dat$ideology.recode.1[stack.dems.dat$state == i]))
    
    mean.perm = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state == i]),na.rm = T)
    cis.perm = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state == i]))
    
    mean.camp = mean(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state != i]),na.rm = T)
    cis.camp = ci(as.numeric(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state != i]))
    
    GCP.bars = vector(length = 20)
    unds = c(1,5,9,13,17)
    dems = c(2,6,10,14,18)
    perms = c(3,7,11,15,19)
    camps = c(4,8,12,16,20)
      
    GCP.bars[unds] = table(stack.unds.dat$ideology.recode.1[stack.unds.dat$state == i])/nrow(subset(stack.unds.dat,is.na(stack.unds.dat$ideology.recode.1) == F & stack.unds.dat$state == i))
    GCP.bars[dems] = table(stack.dems.dat$ideology.recode.1[stack.dems.dat$state == i])/nrow(subset(stack.dems.dat,is.na(stack.dems.dat$ideology.recode.1) == F & stack.dems.dat$state == i))
    GCP.bars[perms] = table(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state == i])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$camp.state == i & GCP.dat$perm.state == i))
    GCP.bars[camps] = table(GCP.dat$ideology.recode.1[GCP.dat$camp.state == i & GCP.dat$perm.state != i])/nrow(subset(GCP.dat,is.na(GCP.dat$ideology.recode.1) == F & GCP.dat$camp.state == i & GCP.dat$perm.state != i))
    
    bars.out = barplot(GCP.bars,
                       col = rep(c('black','white','black',gray.color),5),
                       density = rep(c(20,300,300,300),4),
                       ylim = ylims,
                       xlab = '',
                       yaxt = 'n',
                       xaxt = 'n',
                       beside = T,
                       space = rep(c(.5,0,0,0),5),
                       cex.axis = axis.cex)
    
    ##add legend for ohio and colorada only becauase those will be highlighted
    if(i %in% c('OH','CO')){
      legend(x = 'topright',		
             legend = c('Undecideds', 'Democrats','In-State Workers', 'Out-of-State Workers'),
             fill = rep(c('black','white','black',gray.color),5),
             density = rep(c(20,300,300,300),4),
             bty = 'n',
             cex = 1
        )
      }
    
    bars.places = (bars.out[unds]+bars.out[perms+1])/2
    
    mtext(names.args[c(1,2,4,5)], side = 1, at = bars.places[c(1,2,4,5)],
          cex = ifelse(i == 'OH', .95, lab.cex),
          line = 1)
    mtext(names.args[3], side = 1, at = bars.places[3],
          cex = ifelse(i == 'OH', .95, lab.cex),
          line = .25)
    axis(side = 2,
         at = seq(from = ylims[1], to = ylims[2], by = .2)
      )
    
  
    plot(1:5,1:5,
         type = 'n',
         ylim = c(.75,1),
         xlim = c(1,5),
         xlab = '',
         ylab = '',
         yaxt = 'n',
         xaxt = 'n'
      )
    axis(side = 1,
         at = seq(from = 1, to = 5, by = .5),
         cex = axis.cex)
    
    points(mean.und,
           1,
           col = 'black',
           pch = 18,
           cex = ifelse(i == 'OH', 1.25, point.cex)
        )
    segments(x0 = cis.und$lower95ci, x1 = cis.und$upper95ci,
             y0 = 1, y1 = 1,
             col = 'black')
    text(mean.und + offset,
         1 ,
         'Undecideds',
         pos = 4,
         cex = ifelse(i == 'OH', 1, text.cex)
        )
    
    points(mean.dem,
           .925,
           col = 'black',
           pch = 18,
           cex = ifelse(i == 'OH', 1.25, point.cex)
        )
    segments(x0 = cis.dem$lower95ci, x1 = cis.dem$upper95ci,
             y0 = .925, y1 = .925,
             col = 'black')
    text(mean.dem + offset,
         .925 ,
         'Democrats',
         pos = 4,
         cex = ifelse(i == 'OH', 1, text.cex)
      )
      
    points(mean.perm,
           .85,
           col = 'black',
           pch = 18,
           cex = ifelse(i == 'OH', 1.25, point.cex)
        )
    segments(x0 = cis.perm$lower95ci, x1 = cis.perm$upper95ci,
             y0 = .85, y1 = .85,
             col = 'black')
    text(mean.perm + offset,
         .85,
         'In State Workers',
         pos = 4,
         cex = ifelse(i == 'OH', 1, text.cex)
        )
    
    points(mean.camp,
           .775,
           col = 'black',
           pch = 18,
           cex = ifelse(i == 'OH', 1.25, point.cex)
        )
    segments(x0 = cis.camp$lower95ci, x1 = cis.camp$upper95ci,
             y0 = .775, y1 = .775,
             col = 'black')
    text(mean.camp + offset,
         .775 ,
         'Out of State Workers',
         pos = 4,
         cex = ifelse(i == 'OH', 1, text.cex)
      )
    
    dev.off()
    
  }	



##########################################################################
##########################################################################
##########################################################################
###FIGURE A1, PAGE 270

cat('Creating Figure A1...\n \n \n')

#####################################
demos = c('strong.partisan','female','white','black','hispanic','college.track','non.church','income.recode','age')
plot.names = c('Strong Partisan', 'Female', 'White', 'Black', 'Hispanic','College Track', 'Non Church', 'Yearly Income', 'Age')

pdf('output/figure_A1.pdf')
  par(mfrow = c(3,3))
  par(las = 1)
  for(i in demos){
    stack.frame = tapply(stack.dems.dat[,i],
                         stack.dems.dat$state,
                         FUN = mean,
                         na.rm = T
                        )
    stack.und.frame = tapply(stack.unds.dat[,i],
                             stack.unds.dat$state,
                             FUN = mean,
                             na.rm = T
                          )
    
    GCP.camp.frame = tapply(GCP.dat[,i],
                            GCP.dat$camp.state,
                            FUN = mean,
                            na.rm = T
                            )
    all.frame = merge(stack.frame, GCP.camp.frame,
                      by = 'row.names'
                      )
    all.frame = merge(all.frame,stack.und.frame,
                      by.x = 'Row.names',
                      by.y = 'row.names'
                      )
    
    colnames(all.frame) = c('state','stack.mean','GCP.mean.camp','stack.und.mean')			
    all.frame = all.frame[all.frame$state%in%battles,] ##limit to battleground states
        
    if(i == 'income.recode'){
      xlims = c(40000,100000)
      ylims = xlims 
      }
    if(i == 'age'){ 
      xlims = c(30,60)
      ylims = xlims 
      }
    if(!i %in%c('income.recode','age')){
      xlims = c(0,1)
      ylims = c(0,1)
      }
    
    plot(all.frame$stack.mean,all.frame$GCP.mean.camp,
         type = 'n',
         main = plot.names[which(demos == i)],
         xlab = 'Democrats and Undecided Voters',
         ylab = 'Campaign Workers',
         ylim = ylims,
         xlim = xlims)
    abline(a = 0, b = 1,
           lty = 2)
    text(all.frame$stack.mean,all.frame$GCP.mean.camp,
         labels = all.frame$state,
         cex = .75,
         col = 'blue'
        )
    text(all.frame$stack.und.mean,all.frame$GCP.mean.camp,
         labels = all.frame$state,
         cex = .75,
         col = gray.color
       )
      }
  dev.off()




##########################################################################
##########################################################################
##########################################################################
###Figure A2, page 272

cat('Creating Figure A2...\n \n \n')

CCES.dems.dat.college = CCES.dems.dat[CCES.dems.dat$educ %in% c('4-year','Post-grad'),]
CCES.dems.dat.college.table = table(CCES.dems.dat.college$YAL321)[order(table(CCES.dems.dat.college$YAL321), decreasing = T)] #order table by counts to get ranking
CCES.dems.dat.college.table.ordering = names(CCES.dems.dat.college.table) ##extract names
CCES.dems.dat.college$YAL321.rc  = factor(CCES.dems.dat.college$YAL321, CCES.dems.dat.college.table.ordering) ##use 

CCES.dems.dat.college.most.imp.vote.N = length(CCES.dems.dat.college$YAL321.rc[is.na(CCES.dems.dat.college$YAL321.rc)==F & CCES.dems.dat.college$YAL321.rc != negative.phrase.CCES.dat])

CCES.dems.dat.college.vote.table = table(CCES.dems.dat.college$YAL321.rc)/CCES.dems.dat.college.most.imp.vote.N

CCES.dems.dat.college.vote.table =  CCES.dems.dat.college.vote.table[1:5]

###Figure A2, page 272
pdf('output/figure_A2.pdf',
    width = 9.5, height = 5.5)
  names.cex = 1.5 ##names text size
  par(las = 1)
  xbars = barplot(CCES.dems.dat.college.vote.table,
                  ylim = c(0,.9),
                  cex.names = names.cex,
                  col = gray.color,
                  border = NA,
                  names = c('Economy','Health Care','Inequality','Other','Inflation'))
  text(y = CCES.dems.dat.college.vote.table,
       x = xbars,
       round(CCES.dems.dat.college.vote.table,2),
       cex = 1.35,
       pos = 3)
  title(	main = 'Issue Thought to be Most Important to Other Voters \n by College Educated Democrat Voters',
         cex.main = 2)
  dev.off()



##########################################################################
##########################################################################
##########################################################################
###Figure A3, page 273

cat('Creating Figure A3...\n \n \n')

issues = c('pro.choice','affirmative.action','global.warming.believe','gay.marriage')
plot.names = c('Abortion','Affirmative Action','Global Warming','Gay Marriage')

pdf('output/figure_A3.pdf')
    par(mfrow = c(2,2))
    par(las = 1)
    for(i in issues){
      ##find predictions
      form = paste(i, '~ liberal.all + moderate +  vote.2010+female + +white+black+hispanic+college.track+non.church+income.recode+age+strong.dem + factor(state)')
      predict.reg = glm(formula(form), 
                        data = CCAP.com.dems.dat,
                        family = binomial(link = 'logit'))
      
      predict.issue = predict(predict.reg,
                              type = "response")  ##make predictions
      kept.states = unique(CCAP.com.dems.dat$state[as.numeric(names(predict.issue))]) ##have to use only states that are used so can be used in prediction below
      CCAP.com.dems.dat.predictions = merge(CCAP.com.dems.dat,predict.issue, by = 'row.names')
      CCAP.com.dems.dat.predictions = reshape::rename(CCAP.com.dems.dat.predictions, c(y = 'predict.issue')) ##rename for consistency
      
      predict.issue = predict(predict.reg, GCP.dat[GCP.dat$state %in% kept.states,],
                              type = "response") ##make predictions on workers, ensure data has same levels in the data parameter
      GCP.dat.predictions = merge(GCP.dat,predict.issue,
                                  by = 'row.names')
      GCP.dat.predictions = reshape::rename(GCP.dat.predictions, c(y = 'predict.issue')) ##rename for consistency
      
      ##do the same thing for undecided voters
      predict.und.reg = glm(formula(form), 
                            data = CCAP.com.unds.dat,
                            family = binomial(link = 'logit'))
      predict.issue = predict(predict.und.reg,
                              type = "response")  ##make predictions
      CCAP.com.und.dat.predictions = merge(CCAP.com.unds.dat,predict.issue,
                                           by = 'row.names')
      CCAP.com.und.dat.predictions = reshape::rename(CCAP.com.und.dat.predictions, c(y = 'predict.issue'))
      
      ##get national means
      CCAP.dem.mean = mean(CCAP.com.dems.dat.predictions$predict.issue,na.rm = T)
      CCAP.und.mean = mean(CCAP.com.und.dat.predictions$predict.issue,na.rm = T)
      GCP.mean = mean(GCP.dat.predictions$predict.issue,na.rm = T)
      
      
      ###now collapse by state
      CCAP.frame = tapply(CCAP.com.dems.dat.predictions$predict.issue,
                          CCAP.com.dems.dat.predictions$state,
                          FUN = mean,
                          na.rm = T
                          )
      CCAP.und.frame = tapply(CCAP.com.und.dat.predictions$predict.issue,
                              CCAP.com.und.dat.predictions$state,
                              FUN = mean,
                              na.rm = T
                              )
      
      GCP.camp.frame = tapply(GCP.dat.predictions$predict.issue,
                              GCP.dat.predictions$camp.state,
                              FUN = mean,
                              na.rm = T
                            )
      all.frame = merge(CCAP.frame, GCP.camp.frame,
                        by = 'row.names'
                            )
      all.frame = merge(all.frame,CCAP.und.frame,
                        by.x = 'Row.names',
                        by.y = 'row.names'
                          )
  
      colnames(all.frame) = c('state','CCAP.prediction','GCP.prediction.camp','CCAP.und.prediction')  		

      all.frame = all.frame[all.frame$state%in%battles,]
      
      plot(all.frame$CCAP.prediction,all.frame$GCP.prediction.camp,
           type = 'n',
           main = plot.names[which(issues==i)],
           xlab = 'Democrats and Undecided Voters',
           ylab = 'Campaign Workers',
           ylim = c(0,1),
           xlim = c(0,1))
      abline(a = 0, b = 1,
             lty = 3,
             lwd = 1.25)
      abline(h = GCP.mean,
             lty = 2,
             lwd = 1.25,
             col = 'black'
            )
      abline(v = CCAP.dem.mean,
             lty = 2,
             lwd = 1.25,
             col = 'blue'
            )
      abline(v = CCAP.und.mean,
             lty = 2,
             lwd = 1.25,
             col = gray.color
            )
      
      text(all.frame$CCAP.prediction,all.frame$GCP.prediction.camp,
           labels = all.frame$state,
           cex = .75,
           col = 'blue'
            )
      text(all.frame$CCAP.und.prediction,all.frame$GCP.prediction.camp,
           labels = all.frame$state,
           cex = .75,
           col = gray.color
            )
          }
    dev.off()
    


##########################################################################
##########################################################################
############################################################################
###TABLE A6, Page 276 
cat('Creating Table A6\n \n \n')

###subset by date between early and late takers of GCP
GCP.dat.early = GCP.dat[GCP.dat$fullstartdate < '2012-10-01',]
GCP.dat.late = GCP.dat[GCP.dat$fullstartdate >= '2012-10-01',]

outmat = as.data.frame(matrix(nrow = length(var.names), ncol = 5)) ##storage matrix

##loop through vars, put each in table:
for(i in 1:length(var.names)){
  outmat[i,1] = var.names[i]
  outmat[i,2] = mean(GCP.dat[,var.names[i]], na.rm = T)
  outmat[i,3] = mean(GCP.dat.early[,var.names[i]], na.rm = T)
  outmat[i,4] =  mean(GCP.dat.late[,var.names[i]], na.rm = T)
  outmat[i,5] = t.test(GCP.dat.early[,var.names[i]],GCP.dat.late[,var.names[i]])$p.value
    } 

colnames(outmat) = c('variable','Workers','Workers Before Oct','Workers Oct and Later','P Value')

##turn into latex table
outmat[,2:ncol(outmat)] = round(outmat[,2:ncol(outmat)],3)
outmat[1:10,2:(ncol(outmat)-1)] =  outmat[1:10,2:(ncol(outmat)-1)]*100  
outmat.print = rbind(outmat,c('N',nrow(GCP.dat),nrow(GCP.dat.early),nrow(GCP.dat.late),'')) ##add N.  

outtable = xtable(outmat.print)
print(outtable,
      file = 'output/table_A6.tex',
      include.rownames = F)	



##########################################################################
##########################################################################
############################################################################
###TABLE A7, Page 276 
##DOWNBALLOT WORKERS
cat('Creating Table A7\n \n \n')

###summary stats
outmat = as.data.frame(matrix(nrow = length(var.names), ncol = 3)) ##storage matrix
outmat.t.test = as.data.frame(matrix(nrow = length(var.names), ncol = 2)) ##storage 
##loop through vars, put each in table:
for(i in 1:length(var.names)){
  outmat[i,1] = var.names[i]
  outmat[i,2] = mean(GCP.dat[,var.names[i]], na.rm = T)
  outmat[i,3] = mean(GCP.downballot.dat[,var.names[i]], na.rm = T)
  
  ##make table of t.test results too	
  outmat.t.test[i,1] = var.names[i]
  outmat.t.test[i,2] = t.test(GCP.downballot.dat[,var.names[i]], GCP.dat[,var.names[i]])$p.value
    } 

##turn into latex table
outmat[1:10,2:ncol(outmat)] =  outmat[1:10,2:ncol(outmat)]*100  
outmat = cbind(outmat,outmat.t.test[2])
colnames(outmat) = c('variable','Obama Workers','Downballot Workers','P Value')
outmat[,2:ncol(outmat)] = round(outmat[,2:ncol(outmat)],3)
outmat.print = rbind(outmat,c('N', nrow(GCP.dat),nrow(GCP.downballot.dat))) ##add N.  

outtable = xtable(outmat.print)
print(outtable,
      file = 'output/table_A7.tex',
      include.rownames = F)	

